<?php
	//error_reporting(0);
    require_once "data/handler.php";
    session_start();
    //$session = new SessionControl();
    
    if(isset($_POST['submitlogin'])){
        $error = allowLogin($_POST);
    
        if($error == ""){
            //$_SESSION['allow'] = 1;
            //$session->Open();
            $_SESSION["isLoggedIn"]=true;
            $_SESSION["uname"]=$_POST['emaillog'];
            header('location: '.$GLOBALS['indexpath']);
        }
    }
    else if(isset($_POST['submitreg'])){
        $_POST = sanitize($_POST);
        $error = accountExists($_POST);
        if($error == ""){
            saveAccount($_POST);
        }
    }
    else if(!isset($_SESSION["isLoggedIn"]) || !($_SESSION["isLoggedIn"] === true)){
        session_unset();
        session_destroy();
    }
    else if(isset($_SESSION["isLoggedIn"])){
        header('location: '.$GLOBALS['indexpath']);
    }
?>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-6" />
        <title>Musique Basse</title>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/jquery.validate.js"></script>
        <script>
            $(document).ready(function(){
                $("#loginform").validate({
                    rules: {
                        emaillog: {email:true, required:true},
                        password: {minlength:6, required:true}
                    },
                    messages: {
                        emaillog: {email:"Please enter a valid e-mail address",
                                required:"Please enter your e-mail address"},
                        password: {minlength:"Your password must be at least 6 characters long",
                                   required:"Please enter your password"}
                    }
                });
                $("#regform").validate({
                    rules: {
                        fname: {required:true, minlength:2, maxlength: 30},
                        lname: {required:true, minlength:2, maxlength: 30},
                        address: {required:true, minlength:5, maxlength: 60},
                        emailreg: {email:true, required:true},
                        password1: {minlength:6, maxlength: 30, required:true},
                        password2: {minlength:6, maxlength: 30, required:true, equalTo:"#password1"}
                    },
                    messages:{
                        fname: {required:"Please enter your first name",
                                minlength:"Your first name must be at least 2 characters long",
                                maxlength:"Your first name must be at most 30 characters long"},
                        lname: {required:"Please enter your last name",
                                minlength:"Your last name must be at least 2 characters long",
                                maxlength:"Your last name must be at most 30 characters long"},
                        address: {required:"Please enter your address",
                                  minlength:"Your address must be at least 5 characters long",
                                  maxlength:"Your address must be at most 60 characters long"},
                        emailreg: {email:"Please enter a valid e-mail address",
                                required:"Please enter your e-mail address"},
                        password1: {minlength:"Your password must be at least 6 characters long",
                                    maxlength:"Your password must be at most 30 characters long",
                                    required:"Please enter your password"},
                        password2: {minlength:"Your password must be at least 6 characters long",
                                    maxlength:"Your password must be at most 30 characters long",
                                    required:"Please enter your password",
                                    equalTo:"Please enter the same password as above"}
                    }
                });
            });
        </script>
    </head>
    <body>    
        <div id="main" class="page">
            <header>
                <h1><img src="buttons/logo.png"/></h1>
                <h6>A music store for all kinds of music listeners</h6>
                <nav>
                    <a href="index.php"><img src="buttons/home.png"/></a>
                    <a href="products.php"><img src="buttons/products.png"/></a>
                    <a href="#" class="fltright"><img src="buttons/login.png"/></a>
                </nav>
            </header>
            
            <div id="content">
                <div id="login">
                    <h4>LOG IN</h4>
                    <p>Already have an account? Then please input your username and password</p>
                    
                    <?php
                        if(isset($_POST["submitlogin"]))echo '<div class="error">'.$error.'</div>';
                    ?>
                    
                    <form id="loginform" name="loginform" action="" method="post">
                        <ul>
                            <li>
                                <label for="emaillog">E-mail</label><em>*</em>
                                <input type="text" id="emaillog" name="emaillog" />
                            </li>
                            <li>
                                <label for="password">Password</label><em>*</em>
                                <input type="password" id="password" name="password" />
                            </li>
                            <li>
                                <input type="submit" id="submitlogin" name="submitlogin" value="Log in" />
                            </li>
                        </ul>
                    </form>
                </div>

                <div id="register">
                    <h4>REGISTER</h4>
                    <p>Not yet registered? Fill-out the form below and register</p>
                    
                    <?php
                        if(isset($_POST["submitreg"]))echo '<div class="error">'.$error.'</div>';
                    ?>
                    
                    <form id="regform" name="regform" action="" method="post">
                        <ul>
                            <li>
                                <label for="fname">First Name</label><em>*</em>
                                <input type="text" id="fname" name="fname" />
                            </li>
                            <li>
                                <label for="lname">Last Name</label><em>*</em>
                                <input type="text" id="lname" name="lname" />
                            </li>
                            <li>
                                <label for="address">Address</label><em>*</em>
                                <textarea id="address" name="address"></textarea>
                            </li>
                            <li>
                                <label for="emailreg">E-mail</label><em>*</em>
                                <input type="text" id="emailreg" name="emailreg" />
                            </li>
                            <li>
                                <label for="password1">Password</label><em>*</em>
                                <input type="password" id="password1" name="password1" />
                            </li>
                            <li>
                                <label for="password2">Re-type Password</label><em>*</em>
                                <input type="password" id="password2" name="password2" />
                            </li>
                            <li>
                                <input type="submit" id="submitreg" name="submitreg" value="Register" />
                            </li>
                        </ul>
                    </form>
                </div>
            </div>
            <div class="clear"></div>
            
            <footer>
                <p>Copyright &copy; 2012 KENNICS. All rights reserved.</p>
                <p>Design by Werdnazner</p>
            </footer>
        </div>
    </body>
</html>